//
//  ControlUtil.m
//  SKStock
//
//  Created by thPark on 11. 1. 6..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "ControlUtil.h"


@implementation ControlUtil

UIAlertView* alertProgress = nil;

+ (UIColor *)colorWithHexString:(NSString *)hex {
	if([hex length] != 6)
		return nil;
	
	const char * cHex = [hex cStringUsingEncoding:NSUTF8StringEncoding];
	int red, green, blue;
	sscanf(cHex, "%02x%02x%02x", &red, &green, &blue);
	UIColor * color = [UIColor colorWithRed:(red / 255.0) green:(green / 255.0) blue:(blue / 255.0) alpha:1];
	return color;
}

+ (BOOL)point:(CGPoint)point inRect:(CGRect)rect {
	if(point.x >= rect.origin.x &&
	   point.x < (rect.origin.x + rect.size.width) &&
	   point.y >= rect.origin.y &&
	   point.y < (rect.origin.y + rect.size.height))
		return YES;
	return NO;
}

+ (void)showAlertCaution:(NSString*)title body:(NSString*)body btn:(NSString*)btn delegate:(id)del tag:(int)tag;{
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title 
													message:body
												   delegate:del 
										  cancelButtonTitle:btn 
										  otherButtonTitles: nil];
	alert.tag = tag;
	[alert show];
	[alert release];	
}

+ (void)showAlertCaution:(NSString*)title body:(NSString*)body delegate:(id)del tag:(int)tag
{
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title 
													message:body
												   delegate:del 
										  cancelButtonTitle:@"OK" 
										  otherButtonTitles: nil];
	alert.tag = tag;
	[alert show];
	[alert release];	
}

+ (void)showAlertConform:(NSString*)title body:(NSString*)body delegate:(id)del tag:(int)tag
{
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title 
													message:body
												   delegate:del 
										  cancelButtonTitle:nil 
										  otherButtonTitles:@"예", @"아니오", nil];
	alert.tag = tag;
	[alert show];
	[alert release];	
}

+ (void)showAlertConform:(NSString*)title body:(NSString*)body lBtn:(NSString*)lBtn rBtn:(NSString*)rBtn delegate:(id)del tag:(int)tag{
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title 
													message:body
												   delegate:del 
										  cancelButtonTitle:nil 
										  otherButtonTitles:lBtn, rBtn, nil];
	alert.tag = tag;
	[alert show];
	[alert release];	
}

+ (void)showAlertCaution:(NSString*)title body:(NSString*)body btn:(NSString*)btn delegate:(id)del;{
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title 
													message:body
												   delegate:del 
										  cancelButtonTitle:btn 
										  otherButtonTitles: nil];
	[alert show];
	[alert release];	
}

+ (void)showAlertCaution:(NSString*)title body:(NSString*)body delegate:(id)del{
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title 
													message:body
												   delegate:del 
										  cancelButtonTitle:@"OK" 
										  otherButtonTitles: nil];
	[alert show];
	[alert release];	
}

+ (void)showAlertConform:(NSString*)title body:(NSString*)body delegate:(id)del{
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title 
													message:body
												   delegate:del 
										  cancelButtonTitle:nil 
										  otherButtonTitles:@"예", @"아니오", nil];
	[alert show];
	[alert release];
}

+ (void)showAlertProgress:(NSString*)title body:(NSString*)body delegate:(id)del{
	alertProgress = [[UIAlertView alloc] initWithTitle:title message:body delegate:del cancelButtonTitle:nil otherButtonTitles: nil];
	[alertProgress show];
	
	UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
	indicator.center = CGPointMake(284 / 2, 114 - 50);
	//indicator.center = CGPointMake(alertProgress.bounds.size.width / 2, alertProgress.bounds.size.height - 50);
	[indicator startAnimating];
	[alertProgress addSubview:indicator];
	[indicator release];
}

+ (void)hideAlertProgress{
	[alertProgress dismissWithClickedButtonIndex:0 animated:YES];
	[alertProgress removeFromSuperview];
	[alertProgress release];
}

+ (void)alertOk:(NSString *)title message:(NSString *)message {
	UIAlertView * alert = [[UIAlertView alloc] initWithTitle:title
													 message:message
													delegate:nil
										   cancelButtonTitle:@"확인"
										   otherButtonTitles:nil];
	[alert show];
	[alert release];
}

+ (void)alertYesNo:(NSString *)title message:(NSString *)message delegate:(id<UIAlertViewDelegate>)delegate {
	UIAlertView * alert = [[UIAlertView alloc] initWithTitle:title
													 message:message
													delegate:delegate
										   cancelButtonTitle:@"예"
										   otherButtonTitles:@"아니오", nil];
	[alert show];
	[alert release];
}

+ (void)alertYesNoWithText:(NSString *)title message:(NSString *)message delegate:(id<UIAlertViewDelegate>)delegate yesText:(NSString *)yesText noText:(NSString *)noText {
	UIAlertView * alert = [[UIAlertView alloc] initWithTitle:title
													 message:message
													delegate:delegate
										   cancelButtonTitle:yesText
										   otherButtonTitles:noText, nil];
	[alert show];
	[alert release];
}

+ (void)showAlertEdit:(NSString*)title hint:(NSString*)hint delegate:(id)del{
	UIAlertView* alert = [[UIAlertView alloc] initWithTitle:title
													message:@"\n"
												   delegate:del
										  cancelButtonTitle:@"예"
										  otherButtonTitles:@"아니오", nil];
	
	UITextField* textField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 50.0, 260.0, 25.0)]; 
	[textField setBackgroundColor:[UIColor whiteColor]];
	[textField setPlaceholder:hint];
	[textField setDelegate:self];
	[textField setReturnKeyType:UIReturnKeyDone];
	[alert addSubview:textField];
	[textField becomeFirstResponder];
	
	[alert show];
	[alert release];
	[textField release];
}

+ (void)showAlertEdit:(NSString*)title hint:(NSString*)hint delegate:(id)del textDel:(id)textDel tag:(int)tag{
	UIAlertView* alert = [[UIAlertView alloc] initWithTitle:title
													message:@"\n"
												   delegate:del
										  cancelButtonTitle:@"예"
										  otherButtonTitles:@"아니오", nil];
	
	alert.tag = tag;
	UITextField* textField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 50.0, 260.0, 25.0)]; 
	[textField setBackgroundColor:[UIColor whiteColor]];
	[textField setPlaceholder:hint];
	[textField setDelegate:textDel];
	[textField setReturnKeyType:UIReturnKeyDone];
	[alert addSubview:textField];
	[textField becomeFirstResponder];
	
	[alert show];
	[alert release];
	[textField release];
}

@end
